package com.nbjtjc.safe.service.emg.handle;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.nbjtjc.safe.bean.Constant;
import com.nbjtjc.safe.bean.MyCache;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.handle.EmgEventMapper;
import com.nbjtjc.safe.mapper.emg.handle.EmgHandleReportMapper;
import com.nbjtjc.safe.mapper.sys.SysNoticeMapper;
import com.nbjtjc.safe.mapper.sys.SysNoticeTargetMapper;
import com.nbjtjc.safe.mapper.sys.SysUserMapper;
import com.nbjtjc.safe.model.emg.handle.EmgHandleReport;
import com.nbjtjc.safe.model.sys.SysNotice;
import com.nbjtjc.safe.model.sys.SysUser;
import com.nbjtjc.safe.service.sys.SysDeptService;
import com.nbjtjc.safe.service.sys.SysNoticeService;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EmgHandleReportService {
    @Autowired
    private EmgHandleReportMapper emgHandleReportMapper;

    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private EmgEventMapper emgEventMapper;

    @Autowired
    private SysNoticeMapper sysNoticeMapper;
    @Autowired
    private SysNoticeTargetMapper sysNoticeTargetMapper;
    @Autowired
    private SysNoticeService sysNoticeService;

    @Autowired
    private MyCache myCache;
    public Result add(EmgHandleReport emgHandleReport) {
        Result result = new Result();
        if (ObjectUtil.isNull(emgHandleReport) || ObjectUtil.isNull(emgHandleReport.getEventId())) {
            result.setFalse("不能为空");
            return result;
        }
        SysUser contactUser = sysUserMapper.selectByUserId(emgHandleReport.getContactUserId());
        SysUser reportUser = MyShiroUtils.getSessionUser();
        emgHandleReport.setContactUserId(contactUser.getUserId());
        emgHandleReport.setReportTime(new Date());
        emgHandleReport.setReportUserId(reportUser.getUserId());
        emgHandleReport.setReportToUnitId(sysDeptService.getParentCompanyId(reportUser.getCompanyId()));
        emgHandleReport.setReportUnitCompanyId(reportUser.getCompanyId().intValue());
        emgHandleReportMapper.insertSelective(emgHandleReport);
        emgEventMapper.setReported(emgHandleReport.getEventId());
        addNotice(emgHandleReport);
        result.setData(emgHandleReport);
        return result;
    }

    public Result update(EmgHandleReport emgHandleReport) {
        Result result = new Result();
        if (ObjectUtil.isNull(emgHandleReport) || ObjectUtil.isNull(emgHandleReport.getId())) {
            result.setFalse("不能为空");
            return result;
        }
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("user");
        emgHandleReport.setContactUserId(sysUser.getUserId());
        emgHandleReport.setReportTime(new Date());
        emgHandleReport.setReportUnitCompanyId(sysUser.getCompanyId().intValue());
        emgHandleReportMapper.updateByPrimaryKeySelective(emgHandleReport);
        result.setData(emgHandleReport);
        return result;
    }

    public Result find(EmgHandleReport emgHandleReport) {
        Result result = new Result();
        PageHelper.startPage(emgHandleReport.getPageCurrent(), emgHandleReport.getPageSize());
        List<EmgHandleReport> emgHandleReports=emgHandleReportMapper.search(emgHandleReport);
        compareNoticeUser(emgHandleReports);
        result.setData(emgHandleReports);
        return result;
    }

    /**
     * 通知相关人员
     *
     * @param emgHandleReport
     */
    private void addNotice(EmgHandleReport emgHandleReport) {
        if (CollectionUtil.isEmpty(emgHandleReport.getNoticeUserIds())) {
            return;
        }
        SysNotice sysNotice = new SysNotice();
        sysNotice.setCreateTime(new Date());
        sysNotice.setWriteUserId(MyShiroUtils.getSessionUserId());
        sysNotice.setCompanyId(MyShiroUtils.getCompanyId());
        sysNotice.setDeptId(MyShiroUtils.getDeptId());
        sysNotice.setRelateId(emgHandleReport.getId());
        sysNotice.setIdType(Constant.SysNoticeType.EMG_HANDLE_REPORT.getValue());
        sysNotice.setNoticeType(Constant.SysNoticeType.EMG_HANDLE_REPORT.getValue());
        sysNotice.setTitle("重大事件上报--请查看" + emgHandleReport.getEventName());
        sysNotice.setContent("请前往查看重大事件" + emgHandleReport.getEventName());
        sysNotice.setNoticeStatus(Constant.SysNoticeStatus.PUBLIC.getValue());
        sysNoticeService.addNoticeList(sysNotice, emgHandleReport.getNoticeUserIds());

    }

    private void compareNoticeUser(List<EmgHandleReport> emgHandleReports) {
        emgHandleReports.forEach(emgHandleReport -> {
            SysNotice sysNotice = new SysNotice();
            sysNotice.setRelateId(emgHandleReport.getId());
            sysNotice.setIdType(Constant.SysNoticeType.EMG_HANDLE_REPORT.getValue());
            sysNotice.setNoticeType(Constant.SysNoticeType.EMG_HANDLE_REPORT.getValue());
            sysNotice = sysNoticeMapper.selectOne(sysNotice);
            if(ObjectUtil.isNotNull(sysNotice)){
                List<Integer> sysNoticeTargets =
                        sysNoticeTargetMapper.findUserIdsByNoticeId(sysNotice.getnId());
                if(CollectionUtil.isNotEmpty(sysNoticeTargets)){
                    sysNoticeTargets.forEach(target->{
                        emgHandleReport.getNoticeUsers().add(myCache.getUser(target));
                    });
                }
            }
        });
    }

}
